package hummer.context;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 14-8-24
 * Time: 下午11:04
 * To change this template use File | Settings | File Templates.
 */
public class CommandFactory {

    private static final ConcurrentMap<String, Command> commandContainer = new ConcurrentHashMap<String, Command>();

    public static Command createCommand(String key) {

        if (commandContainer.containsKey(key)) {

            return commandContainer.get(key);

        }

        Command command = new Command(key);
        commandContainer.putIfAbsent(key, command);
        return command;
    }

    public static List<Command> getCommands() {
        List<Command> list = new ArrayList<Command>();

        Iterator<Map.Entry<String, Command>> iterator = commandContainer.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Command> entry = iterator.next();
            list.add(entry.getValue());
        }

        return list;
    }


}
